package Course16;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.File;
import java.io.*;
import java.util.HashMap;
import java.util.Map;


public class CodeSearch {
    public static void main(String[] args) throws Exception{
        //读取Downloads文件夹gaode.xlsx
        FileInputStream in = new FileInputStream(new File("E:\\gaode.xlsx"));

        Workbook workbook = new XSSFWorkbook(in);

        Sheet sheet = workbook.getSheetAt(0);

        int rowNum = sheet.getLastRowNum();
        Map<String,City> map=new HashMap<>();
        for (
                int i = 2;
                i < rowNum; i++)
        {

            City city=new City();
            Row row = sheet.getRow(i);
            String name = row.getCell(0).getStringCellValue();
            String abcode = row.getCell(1).getStringCellValue();
            city.setName(name);
            city.setAbcode(abcode);
            map.put(city.getAbcode(),city);
            System.out.println(map.get(abcode).getName());
            System.out.println(map.get(abcode).getAbcode());
        }
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
        String string=null;
        System.out.println("输入字符串");
        System.out.println("输入'结束'退出程序");
        do{
            try{
            string=bufferedReader.readLine();
            if (string.equals("结束")){}
            else if(!map.containsKey(string)){
                System.out.println("未找到该邮编所对应的城市，请输入正确的邮编");
            }else {
                City city1 = map.get(string);
                System.out.println("城市的名称是:" + city1.getName());
                System.out.println("城市的邮编是:" + city1.getAbcode());
            }
            }catch (Exception e){
                e.printStackTrace();
            }
           // System.out.println(string);
        }while(!string.equals("结束"));
    }
}
